/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.web.misc.cg.descriptor;

import org.vacoor.nothing.common.util.StringUtils;

/**
 * One To One 关系
 *
 * @author vacoor
 */
public class OneToOne extends ToOne {

    public OneToOne(String source) {
        super(source);
    }

    public String getComputedJoinColumn() {
        String relColumn = null;
        // 如果对方是维护方
        if (StringUtils.hasText(mappedBy)) {
            // 从维护方中查找当前关联配置
            for (OneToOne toOne : rel.getOneToOne()) {
                if (mappedBy.equals(toOne.getProperty())) {
                    relColumn = toOne.getJoinColumn();
                    break;
                }
            }

            if (!StringUtils.hasText(relColumn)) {
                throw new IllegalArgumentException(property + "标注为 OneToOne mappedBy=" + mappedBy + "" + ", 但是不能在" + rel.getFullTypeName() + "中发现 OneToOne 属性:" + mappedBy + " @" + source);
            }
        } else {
            relColumn = getJoinColumn();
        }
        return relColumn;
    }

}
